package com.info3k.client.components;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.info3k.client.Info3K;
import com.info3k.client.commands.address.ShowAddressCommand;
import com.info3k.client.commands.address.UpdateAddressCommand;
import com.info3k.client.domain.Address;
import com.info3k.client.domain.User;

public class AddressRenderer extends Composite {
	
	private Address address;
	
	private ShowAddressCommand showAddressCommand;
	
	private UpdateAddressCommand editAddressCommand;
	
	public CheckBox selected = new CheckBox();
	
	private boolean addressSelected = false;
	
	private HorizontalPanel panel = new HorizontalPanel();
	
	public AddressRenderer(final Address address, boolean selectable, final Info3K info){
		this.setAddress(address);
		initWidget(panel);
		panel.setWidth("100%");
		
		VerticalPanel panel1 = new VerticalPanel();
		panel1.add(selected);
		
		VerticalPanel panel2 = new VerticalPanel();
		panel2.setWidth("100%");
		
		final Label addr = new Label(Info3K.msgs.address() + ": " + address.getAddress());
		final Button showAddress = new Button(Info3K.msgs.showAddress());
		final Button editAddress = new Button(Info3K.msgs.editAddress());
		showAddress.getElement().getStyle().setProperty("cursor", "pointer");
		showAddress.getElement().getStyle().setProperty("margin", "3px");
		showAddress.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				showAddressCommand = new ShowAddressCommand(address);
				showAddressCommand.execute();
			}
		});
		
		if(!selectable){
			editAddress.getElement().getStyle().setProperty("cursor", "pointer");
			editAddress.getElement().getStyle().setProperty("margin", "3px");
			editAddress.addClickHandler(new ClickHandler(){
				public void onClick(ClickEvent event) {
					editAddressCommand = new UpdateAddressCommand(info, address);
					editAddressCommand.execute();
				}
			});
		}

		
		panel2.add(addr);
		
		if(address.getHouseNumber().length()>0) {
			final Label houseNumber = new Label(Info3K.msgs.houseNumber() + ": " + address.getHouseNumber());
			panel2.add(houseNumber);
		}

		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setStylePrimaryName("verClose1");
		
		panel2.add(hPanel);
		
		User uu = info.getLoginManager().getUser();
		if(uu!=null){
			if(uu.getUsername().equals(address.getUser().getUsername())){
				if(!selectable)
					hPanel.add(editAddress);
			}
		}
		hPanel.add(showAddress);
		
		if(selectable) {
			panel.add(panel1);
		}
		panel.add(panel2);
		
		if(selectable){
			panel.setCellWidth(panel1, "10%");
			panel.setCellHorizontalAlignment(panel1, HorizontalPanel.ALIGN_CENTER);
			panel.setCellVerticalAlignment(panel1, HorizontalPanel.ALIGN_MIDDLE);
			panel.setCellWidth(panel2, "90%");
		} else {
			panel.setCellWidth(panel2, "100%");
		}
		
		selected.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if (selected.getValue()) {
					setAddressSelected(true);
					panel.addStyleName("selectedBackground");
				} else {
					setAddressSelected(false);
					panel.removeStyleName("selectedBackground");
				}
			}
		});
	}
	
	public void clickCheckbox(){
		selected.setValue(true);
		setAddressSelected(true);
		panel.addStyleName("selectedBackground");
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddressSelected(boolean addressSelected) {
		this.addressSelected = addressSelected;
	}

	public boolean isAddressSelected() {
		return addressSelected;
	}
}
